//
//  ZH_MineFocusViewController.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/10/22.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_MineFocusViewController.h"
#import "GC_DetailsSegmentView.h"
#import "ZH_MineInstitutionsTableViewCell.h"
#import "ZH_MineTeacherTableViewCell.h"
#import "ZH_MineViewModel.h"
@interface ZH_MineFocusViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak)UIImageView *bgImage;
@property (nonatomic,weak)UIView *bgView;
@property (nonatomic,weak)UIImageView *lookImage;
@property (nonatomic,weak)UITextField *textField;
@property (nonatomic, strong) GC_DetailsSegmentView *detailsSegmentView;
@property (nonatomic,weak)UITableView *tableView;
///详情选择
@property (nonatomic, assign) NSInteger detailsCurrentItem;
@property (nonatomic,strong)ZH_MineViewModel *viewModel;
@end

@implementation ZH_MineFocusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}
- (void)cleanUpData{
    [self setViewModel:nil];
}
- (void)initData{
    [self.viewModel.followArray removeAllObjects];
    WEAKSELF;
    [self.viewModel followResultBlock:^(NSString *code, NSString *message, id data) {
        if ([code isEqualToString:@"0"]) {
            [weakSelf.tableView reloadData];
        }
        else{
            if ([code isEqualToString:@"1002"]) {
                [self showHint:@"登录过期"];
            }
            else{
                [weakSelf showHint:message];
            }
        }
    }];
}
- (void)initUserInterface{
    self.navigationItem.title = @"我的关注";
    self.view.backgroundColor = kCommonBackgroudColor;
    [self.bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
    }];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(FITSCALE(10));
        make.left.equalTo(self.view).offset(FITSCALE(22));
        make.right.equalTo(self.view).offset(FITSCALE(-22));
        make.height.mas_equalTo(FITSCALE(38));
    }];
    [self.lookImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgView);
        make.left.equalTo(self.bgView).offset(FITSCALE(65));
        make.width.mas_equalTo(FITSCALE(15));
        make.height.mas_equalTo(FITSCALE(15));
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgView);
        make.left.equalTo(self.lookImage.mas_right).offset(FITSCALE(7));
        make.right.equalTo(self.bgView);
    }];
    [self.view addSubview:self.detailsSegmentView];
    [self.detailsSegmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgImage.mas_bottom).offset(FITHEIGHT(3));
        make.left.equalTo(self.bgImage);
        make.right.equalTo(self.bgImage);
        make.height.mas_equalTo(FITSCALE(44));
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.detailsSegmentView.mas_bottom).offset(FITSCALE(9));
        make.left.right.bottom.equalTo(self.view);
    }];
}
///输入框 回调 方法
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //    if(self.onEnterTextFieldBlock){
    //        self.onEnterTextFieldBlock(textField.text, textField.tag);
    //    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.textField endEditing:YES];
    return YES;
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.detailsCurrentItem == 0) {
        return self.viewModel.followArray.count;
    }
    else if (self.detailsCurrentItem == 1){
        return self.viewModel.followArray.count;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.detailsCurrentItem == 0) {
        ZH_MineInstitutionsTableViewCell *cell = [ZH_MineInstitutionsTableViewCell cellWithTableView:tableView];
        [cell updataUI:self.viewModel.followArray[indexPath.row]];
        return cell;
    }
    else if (self.detailsCurrentItem == 1){
        ZH_MineTeacherTableViewCell *cell = [ZH_MineTeacherTableViewCell cellWithTableView:tableView];
        [cell updataUI:self.viewModel.followArray[indexPath.row]];
        return cell;
    }
    return nil;
}
#pragma mark - getter,setter
- (UIImageView *)bgImage{
    if (!_bgImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@"img_banner1"];
        [self.view addSubview:_bgImage = image];
    }
    return _bgImage;
}
- (UIView *)bgView{
    if (!_bgView) {
        UIView *view = [UIView createViewWithBackgroundColor:[UIColor colorWithHexString:@"#DFDFDF"]];
        view.layer.cornerRadius = FITSCALE(18);
        view.layer.masksToBounds = YES;
        view.clipsToBounds = YES;
        [self.view addSubview:_bgView = view];
    }
    return _bgView;
}
- (UIImageView *)lookImage{
    if (!_lookImage) {
        UIImageView *image = [UIImageView createImageViewWithName:@"icon_search"];
        [self.view addSubview:_lookImage = image];
    }
    return _lookImage;
}
- (UITextField *)textField
{
    if(!_textField){
        UITextField *textField = [UITextField createTextFieldWithPlaceholder:@"请输入老师姓名、昵称,机构名称" placeholderColor:kFontColorGray delegateTarget:self];
        textField.borderStyle = UITextBorderStyleNone;
        textField.font = kFontSize_28;
        textField.textAlignment = NSTextAlignmentLeft;
        textField.returnKeyType = UIReturnKeyDone;
        [textField setTextColor:kFontColorBlack];
        [self.view addSubview:_textField = textField];
    }
    return _textField;
}
///图文 产品 选择
- (GC_DetailsSegmentView *)detailsSegmentView
{
    if(!_detailsSegmentView){
        _detailsSegmentView = [[GC_DetailsSegmentView alloc] initWithHeight:FITSCALE(44)];
        [_detailsSegmentView setItems:@[@"机构",@"老师"]];
        [_detailsSegmentView setBottomLineImageViewHidden:NO];
        [_detailsSegmentView setSelectItem:0];
        self.detailsCurrentItem = 0;
        WEAKSelf;
        _detailsSegmentView.onSelectedBlock = ^(NSInteger item) {
            if(item == 0){
                weakSelf.detailsCurrentItem = item;
                [weakSelf.viewModel.followParam setObject:@(item + 1) forKey:@"type"];
                [weakSelf initData];
                 [weakSelf.tableView reloadData];
                
            }else if(item == 1){
                weakSelf.detailsCurrentItem = item;
                [weakSelf.viewModel.followParam setObject:@(item + 1) forKey:@"type"];
                [weakSelf initData];
                [weakSelf.tableView reloadData];
            }
            
        };
    }
    return _detailsSegmentView;
}

- (UITableView *)tableView{
    if(!_tableView){
        UITableView *tableView = [UITableView createTableViewWithDelegateTarget:self];
        tableView.backgroundColor = kCommonBackgroudColor;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setCellAutoAdaptationForEstimatedRowHeight:FITHEIGHT(100)];
        [self.view addSubview:_tableView = tableView];
    }
    return _tableView;
}
- (ZH_MineViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[ZH_MineViewModel alloc] init];
    }
    return _viewModel;
}
@end
